package com.dant.managers
{
	import com.dant.interfaces.IPanelsFocusManager;
	
	import flash.display.DisplayObject;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	
	import mx.controls.scrollClasses.ScrollThumb;
	import mx.core.UIComponent;
	import mx.effects.Blur;
	import mx.utils.UIDUtil;

	public class AdvancedPanelFocusManager extends EventDispatcher implements IPanelsFocusManager
	{
		
		private var registeredFocusableObjectsMap:Object = {};
		protected var focusedObject:DisplayObject;
		private var _blurNoFocused:Boolean = false;
		private var blur:Blur;
		
		// determines if the siblings of the focused object should be blured when they are not focused
		[Inspectable(type="Boolean")]
		public function set blurNoFocused(value:Boolean):void
		{
			_blurNoFocused = value;
			if ( _blurNoFocused )
			{
				blur = new Blur ( );
				blur.blurXFrom=0;
				blur.blurXTo = 6;
				blur.blurYFrom=0;
				blur.blurYTo = 6;	
			}
			else
			{
				blur = null;
			}
		}
		
		public function get blurNoFocused():Boolean
		{
			return (_blurNoFocused);
		}

		public function AdvancedPanelFocusManager(target:IEventDispatcher=null)
		{
			super(target);
		}

        protected function registerObjectToFocusMap ( object:UIComponent ):void
        {
        	registeredFocusableObjectsMap [ UIDUtil.getUID(object) ] = object;
        }

        public function registerObjectToFocusManager ( object:UIComponent ):UIComponent
        {
            if ( !objectIsFocusable(object) )
            {
	            object.addEventListener( MouseEvent.MOUSE_DOWN, object_mouseDownHandler,false,100,true );
	            registerObjectToFocusMap ( object );
	        }
            return ( object );
        }

        public function objectIsFocusable ( object:UIComponent ):Boolean
        {
            return ( registeredFocusableObjectsMap [ UIDUtil.getUID(object) ] != null );
        }

        public function objectIsFocused ( object:UIComponent ):Boolean
        {
            return ( focusedObject == object );
        }

        private function unregisterObjectFromFocusMap ( object:UIComponent ):void
        {
            delete (registeredFocusableObjectsMap [ UIDUtil.getUID(object) ]);
        }
        
        public function unregisterObjectToFocus ( object:UIComponent ):UIComponent
        {
        	if ( objectIsFocusable( object ) )
        	{
	            object.removeEventListener( MouseEvent.MOUSE_DOWN, object_mouseDownHandler );
	            unregisterObjectFromFocusMap( object );
            }
            return ( object );
        }

		protected function unblurAll():void
		{
			blur.blurXFrom = 0;
			blur.blurXTo = 1;
			blur.blurYFrom = 0;
			blur.blurYTo = 1;
			for ( var registeredKey:String in registeredFocusableObjectsMap )
			{
				var registeredObject:DisplayObject = registeredFocusableObjectsMap[registeredKey];
                blur.play([registeredObject]);
                registeredObject.alpha = 0.2;
			}
		}

		protected function setFocusOnObject(object:UIComponent):void
		{
			object.setFocus();
			object.parent.setChildIndex(object, object.parent.numChildren - 1);
            focusedObject = object;

			if ( !blurNoFocused )
			{
				return;
			}

			for ( var registeredKey:String in registeredFocusableObjectsMap )
			{
				var registeredObject:DisplayObject = registeredFocusableObjectsMap[registeredKey];
                if ( registeredObject != object )
                {
                    blur.play([registeredObject]);
                    registeredObject.alpha = 0.2;
/*             if ( isFocusedChanged )
            {
                if ( _isFocused)
                {
                    setStyle("headerColors", [_focusedHeaderColorFrom, _focusedHeaderColorTo]);
                    setStyle("borderColor", _focusedBorderColor);
                }
                else
                {
                    setStyle("headerColors", [_notFocusedHeaderColorFrom, _notFocusedHeaderColorTo]);
                    setStyle("borderColor", _notFocusedBorderColor);                    
                }
            } */
                } 
			}

            blur.play([object],true);
            object.alpha = 1;

		}

		protected function object_mouseDownHandler(event:MouseEvent):void 
		{
			if ( event.isDefaultPrevented() )
			{
				return;
			}

			event.preventDefault();
			var object:UIComponent = UIComponent(event.currentTarget);

			// if the mouseDown is not done on the ScrollBar
			if ( ! (event.target is ScrollThumb) )
			{
				setFocusOnObject(object);
			}
		}
	}
}